package com.ylr.user.framework.controller.api;

import com.ylr.user.framework.service.UserService;
import com.ylr.base.common.annotation.Resubmit;
import com.ylr.base.common.dto.NameValue;
import com.ylr.base.common.dto.PageDto;
import com.ylr.user.common.dto.user.UserDto;
import com.ylr.base.common.jsr303.group.Update;
import com.ylr.base.common.po.user.UserPo;
import com.ylr.base.common.tool.Result;
import com.ylr.user.common.vo.user.UserFormVo;
import com.ylr.user.common.vo.user.UserVo;
import com.ylr.user.framework.feign.YuUserBaseClient;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-04-21 11:44:08
 * className: UserController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/api/user")
@AllArgsConstructor
public class UserApiController implements YuUserBaseClient {

    private static final Logger log = LoggerFactory.getLogger(UserApiController.class);

    private UserService userService;

    /**
     * 用户权限校验接口
     * @param userId  用户主键
     * @param module  所属模块
     * @param channel 所属渠道
     * @param uri     接口路径
     * @return 校验结果[true-通过 false-拒绝]
     */
    @GetMapping("/auth")
    public Result<UserPo> auth(@RequestParam("userId") String userId, @RequestParam("module") String module, @RequestParam("channel") String channel, @RequestParam("uri") String uri) {
        log.info("api => 开始用户权限校验操作。userId={} module={} channel={} uri={}", userId, module, channel, uri);

        if (true) {
            UserPo userPo = new UserPo();
            userPo.setId(123456L);
            userPo.setUsername("测试用户");
            return Result.success(userPo);
        }

        UserPo userPo = userService.auth(userId, module, channel, uri);

        log.info("api => 用户权限校验操作结束。userPo={}", userPo);
        return Result.success(userPo);
    }

    /**
     * 保存用户接口
     * <p>
     * 此接口会校验是否重复提交
     * @param userVo 参数对象
     */
    @Resubmit(message = "保存用户")
    @PostMapping("/save")
    public Result<Long> saveUser(UserVo userVo) {
        log.info("api => 开始保存用户操作。{}", userVo);

        Long userId = userService.saveUser(userVo);

        log.info("api => 保存用户操作结束。userId={}", userId);
        return Result.success(userId);
    }

    /**
     * 分页查询用户接口
     * @param userFormVo 参数对象
     * @return 用户分页数据
     */
    @PostMapping("/page/list")
    public Result<PageDto<UserDto>> pageUserList(UserFormVo userFormVo) {
        log.info("api => 开始分页查询用户操作。{}", userFormVo);

        PageDto<UserDto> page = userService.pageUserList(userFormVo);

        log.info("api => 分页查询用户操作结束。pageDto={}", page);
        return Result.success(page);
    }

    /**
     * 获取用户接口
     * @param id 用户主键
     * @return 用户数据
     */
    @GetMapping("/get")
    public Result<UserDto> getUser(@NotNull(message = "用户主键不能为空") Long id) {
        log.info("api => 开始获取用户操作。id={}", id);

        UserDto userDto = userService.getUser(id);

        log.info("api => 获取用户操作结束。{}", userDto);
        return Result.success(userDto);
    }

    /**
     * 批量获取用户接口
     * @param userIdList 用户主键列表
     * @return 用户列表
     */
    @GetMapping("/get-list")
    public Result<List<UserDto>> getUserList(@Size(min = 1, message = "用户主键列表不能为空") @RequestBody List<Long> userIdList) {
        log.warn("api => 开始批量获取用户操作。userIdList={}", userIdList);

        List<UserDto> userList = userService.getUserList(userIdList);

        log.warn("api => 批量获取用户操作结束。userList={}", userList);
        return Result.success(userList);
    }

    /**
     * 获取用户名字映射接口
     * 根据用户主键集合，查询用户名称并进行映射
     * @param userIdSet 用户主键集合
     * @return 用户名字映射
     */
    @GetMapping("/name/map")
    public Result<Map<Long, String>> getUsernameMap(@RequestBody Set<Long> userIdSet) {
        log.info("api => 开始获取用户名字映射操作。userIdSet={}", userIdSet);

        Map<Long, String> usernameMap = userService.getUsernameMap(userIdSet);

        log.info("api => 开始获取用户名字映射操作。usernameMap={}", usernameMap);
        return Result.success(usernameMap);
    }

    /**
     * 获取用户名字列表接口
     * @param userIdSet  用户主键集合
     * @return 用户名字列表
     */
    @GetMapping("/name/list")
    public Result<List<NameValue<String>>> getUsernameList(@RequestBody Set<Long> userIdSet) {
        log.info("api => 开始获取用户名字列表操作。userIdSet={}", userIdSet);

        List<NameValue<String>> usernameList = userService.getUsernameList(userIdSet);

        log.info("api => 开始获取用户名字列表操作。usernameList={}", usernameList);
        return Result.success(usernameList);
    }

    /**
     * 修改用户接口
     * @param userVo 参数对象
     */
    @Resubmit(spEL = "'updateUserBase-'.concat(#userVo.id)", message = "修改用户")
    @PostMapping("/update/base")
    public Result<Object> updateUserBase(@Validated(Update.class) @RequestBody UserVo userVo) {
        log.info("api => 开始修改用户操作。{}", userVo);

        userService.updateUser(userVo);

        log.info("api => 修改用户操作结束");
        return Result.success();
    }

    /**
     * 删除用户接口
     * @param idList 主键列表
     */
    @Resubmit(message = "删除用户")
    @PostMapping("/delete")
    public Result<Integer> deleteUser(@Size(min = 1, message = "主键列表不能为空") @RequestBody List<Long> idList) {
        log.info("api => 开始删除用户操作。idList={}", idList);

        Integer count = userService.deleteUser(idList);

        log.info("api => 删除用户操作结束。count={}", count);
        return Result.success();
    }

}
